#include "IAInterfaceDX10.h"
#include "Engine/AuxTypesDX10.h"

CIAInterfaceDX10::CIAInterfaceDX10( )
{
    m_pIndexBuffer = NULL;
}

CIAInterfaceDX10::~CIAInterfaceDX10( )
{
    SAFE_RELEASE(m_pIndexBuffer);
    for (size_t i = 0; i < m_VertexBuffers.size(); i++)
    {
        SAFE_RELEASE(m_VertexBuffers[i]);
    }
}

mBool CIAInterfaceDX10::CreateIndexBuffer( mVoid *a_pBufData, mUInt a_uBufSize, TVariableType a_IndexType )
{
    HRESULT hr;
    D3D10_BUFFER_DESC BufferDesc =
    {
        a_uBufSize,
        D3D10_USAGE_DEFAULT,
        D3D10_BIND_INDEX_BUFFER,
        0, 0
    };
    D3D10_SUBRESOURCE_DATA BufInitData;
    ZeroMemory(&BufInitData, sizeof(D3D10_SUBRESOURCE_DATA));

    BufInitData.pSysMem = a_pBufData;
    hr = g_D3DDevice.pD3D10Device->CreateBuffer( &BufferDesc, &BufInitData, &m_pIndexBuffer );
    if (hr != S_OK)
        return false;
    m_IndexFormat = DX10VarType(a_IndexType);

    return true;
}

mUInt CIAInterfaceDX10::CreateVertexBuffer( mVoid *a_pBufData, mUInt a_uBufSize, mUInt a_uStride )
{
    HRESULT hr;
    ID3D10Buffer *pBuf = NULL;
    D3D10_BUFFER_DESC BufferDesc =
    {
        a_uBufSize,
        D3D10_USAGE_DEFAULT,
        D3D10_BIND_VERTEX_BUFFER,
        0, 0
    };
    D3D10_SUBRESOURCE_DATA BufInitData;
    ZeroMemory(&BufInitData, sizeof(D3D10_SUBRESOURCE_DATA));

    BufInitData.pSysMem = a_pBufData;
    hr = g_D3DDevice.pD3D10Device->CreateBuffer( &BufferDesc, &BufInitData, &pBuf );
    if (hr != S_OK)
        return false;
    
    m_VertexBuffers.push_back(pBuf);
    m_Strides.push_back(a_uStride);
    m_Offsets.push_back(0);
    return m_VertexBuffers.size() - 1;
}

mVoid CIAInterfaceDX10::IASetupBuffers( )
{
    if (m_pIndexBuffer)
        g_D3DDevice.pD3D10Device->IASetIndexBuffer(m_pIndexBuffer, m_IndexFormat, 0);
    g_D3DDevice.pD3D10Device->IASetVertexBuffers(0, m_VertexBuffers.size(), &m_VertexBuffers[0], 
        &m_Strides[0], &m_Offsets[0]);
}